{
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "#!pip install arcgis"
      ],
      "metadata": {
        "id": "5gxc4ymkh9Sb"
      },
      "id": "5gxc4ymkh9Sb",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5e5cdc11",
      "metadata": {
        "id": "5e5cdc11"
      },
      "outputs": [],
      "source": [
        "import arcgis\n",
        "import getpass\n",
        "username = input (\"Username:\")\n",
        "password = getpass.getpass (\"Password:\")\n",
        "\n",
        "gis = arcgis.gis.GIS(\"https://datalchemy.maps.arcgis.com\", username, password)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "45864f37",
      "metadata": {
        "id": "45864f37"
      },
      "outputs": [],
      "source": [
        "from arcgis.gis import GIS\n",
        "# Create a GIS object, as an anonymous user for this example\n",
        "gis = GIS()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "910d4362",
      "metadata": {
        "id": "910d4362"
      },
      "outputs": [],
      "source": [
        "import arcgis\n",
        "from arcgis.gis import GIS\n",
        "from IPython.display import display\n",
        "\n",
        "gis = GIS()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9e6f2d06",
      "metadata": {
        "id": "9e6f2d06"
      },
      "outputs": [],
      "source": [
        "items = gis.content.search(\"Landsat 9 Views\", item_type=\"Imagery Layer\", max_items=2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c72d8fb1",
      "metadata": {
        "scrolled": true,
        "id": "c72d8fb1"
      },
      "outputs": [],
      "source": [
        "\n",
        "for item in items:\n",
        "    display(item)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d20f175c",
      "metadata": {
        "id": "d20f175c"
      },
      "outputs": [],
      "source": [
        "items"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "704952f9",
      "metadata": {
        "id": "704952f9"
      },
      "outputs": [],
      "source": [
        "#img_svc_url = 'https://server6.tplgis.org/arcgis6/rest/services/heat_severity_2019/ImageServer'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d6020b7d",
      "metadata": {
        "id": "d6020b7d"
      },
      "outputs": [],
      "source": [
        "img_svc_url ='https://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "df7c2e84",
      "metadata": {
        "id": "df7c2e84"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster import ImageryLayer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d455fe3e",
      "metadata": {
        "id": "d455fe3e"
      },
      "outputs": [],
      "source": [
        "landsat_ms = ImageryLayer(img_svc_url)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d4d10aac",
      "metadata": {
        "id": "d4d10aac"
      },
      "outputs": [],
      "source": [
        "landsat_ms.properties.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3826dc48",
      "metadata": {
        "id": "3826dc48"
      },
      "outputs": [],
      "source": [
        "landsat_ms.properties['description']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "20e64457",
      "metadata": {
        "id": "20e64457"
      },
      "outputs": [],
      "source": [
        "landsat_ms.properties.capabilities"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "edaa0527",
      "metadata": {
        "id": "edaa0527"
      },
      "outputs": [],
      "source": [
        "landsat_ms.properties.allowedMosaicMethods"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "effc416a",
      "metadata": {
        "id": "effc416a"
      },
      "outputs": [],
      "source": [
        "{\n",
        "  \"rasterFunction\": \"Colormap\",\n",
        "  \"rasterFunctionArguments\": {\n",
        "    \"ColormapName\": \"Random\"\n",
        "  },\n",
        "  \"variableName\": \"Raster\"\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eff852d3",
      "metadata": {
        "id": "eff852d3"
      },
      "outputs": [],
      "source": [
        "for fn in landsat_ms.properties.rasterFunctionInfos:\n",
        "    print(fn['name'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bbef7b19",
      "metadata": {
        "scrolled": false,
        "id": "bbef7b19"
      },
      "outputs": [],
      "source": [
        "map = gis.map(\"Chicago,Illinois\", zoomlevel=13)\n",
        "map"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "04878f11",
      "metadata": {
        "id": "04878f11"
      },
      "outputs": [],
      "source": [
        "map.add_layer(landsat_ms, {'renderer':'ClassedSizeRenderer','opacity':0.75})\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2ce3f634",
      "metadata": {
        "id": "2ce3f634"
      },
      "outputs": [],
      "source": [
        "import time\n",
        "from arcgis.raster.functions import apply\n",
        "\n",
        "for fn in landsat_ms.properties.rasterFunctionInfos:\n",
        "    print(fn['name'])\n",
        "    map.remove_layers()\n",
        "    map.add_layer(apply(landsat_ms, fn['name']))\n",
        "    time.sleep(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f55e26cf",
      "metadata": {
        "id": "f55e26cf"
      },
      "outputs": [],
      "source": [
        "savi_map = gis.map(\"Chicago\", zoomlevel=6)\n",
        "savi_map"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7c60ccd3",
      "metadata": {
        "id": "7c60ccd3"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import savi\n",
        "\n",
        "savi_map.add_layer(savi(landsat_ms, band_indexes=\"5 4 0.3\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9a1d579e",
      "metadata": {
        "id": "9a1d579e"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import *\n",
        "\n",
        "land_water = stretch(extract_band(landsat_ms, [4, 5, 3]),\n",
        "                     stretch_type='PercentClip',\n",
        "                     min_percent=2, \n",
        "                     max_percent=2,\n",
        "                     dra=True, \n",
        "                     gamma=[1, 1, 1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "39048b72",
      "metadata": {
        "id": "39048b72"
      },
      "outputs": [],
      "source": [
        "map2 = gis.map(\"Chicago\", zoomlevel=13)\n",
        "map2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "850072d5",
      "metadata": {
        "id": "850072d5"
      },
      "outputs": [],
      "source": [
        "#map.add_layer(landsat_urbanheat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "35a01eec",
      "metadata": {
        "id": "35a01eec"
      },
      "outputs": [],
      "source": [
        "map2.add_layer(land_water)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "47a8bfc9",
      "metadata": {
        "id": "47a8bfc9"
      },
      "outputs": [],
      "source": [
        "\n",
        "for fn in landsat_urbanheat.properties.rasterFunctionInfos:\n",
        "    print(fn['name'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cdcae5ec",
      "metadata": {
        "id": "cdcae5ec"
      },
      "outputs": [],
      "source": [
        "yellow2red?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ab9ca1b6",
      "metadata": {
        "id": "ab9ca1b6"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import *\n",
        "\n",
        "land_water = stretch(extract_band(landsat_urbanheat, [4, 5, 3]),\n",
        "                     stretch_type='PercentClip',\n",
        "                     min_percent=2, \n",
        "                     max_percent=2,\n",
        "                     dra=True, \n",
        "                     gamma=[1, 1, 1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0fe2564c",
      "metadata": {
        "id": "0fe2564c"
      },
      "outputs": [],
      "source": [
        "map2 = gis.map(\"Chicago\", zoomlevel=13)\n",
        "map2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5c7c9d40",
      "metadata": {
        "id": "5c7c9d40"
      },
      "outputs": [],
      "source": [
        "\n",
        "map2.add_layer(land_water)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "50ef570c",
      "metadata": {
        "id": "50ef570c"
      },
      "outputs": [],
      "source": [
        "# Establish a connection to your GIS.\n",
        "from arcgis.gis import GIS\n",
        "from IPython.display import display\n",
        "gis = GIS() # anonymous connection to www.arcgis.com"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c55ecdfe",
      "metadata": {
        "id": "c55ecdfe"
      },
      "outputs": [],
      "source": [
        "from arcgis.features import FeatureLayerCollection"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "13a70f54",
      "metadata": {
        "id": "13a70f54"
      },
      "outputs": [],
      "source": [
        "# Search for 'USA major cities' feature layer collection\n",
        "search_results = gis.content.search('title: USA Major Cities',\n",
        "                                    'Feature Layer')\n",
        "\n",
        "# Access the first Item that's returned\n",
        "major_cities_item = search_results[0]\n",
        "\n",
        "major_cities_item"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d44cec9a",
      "metadata": {
        "id": "d44cec9a"
      },
      "outputs": [],
      "source": [
        "# connect to GIS\n",
        "from arcgis.gis import GIS\n",
        "gis = GIS(\"portal url\", \"username\", \"password\")\n",
        "ports_item = gis.content.get(\"b0cb0c9f63e74e8480af0286eb9ac01f\")\n",
        "ports_item"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8bd15676",
      "metadata": {
        "id": "8bd15676"
      },
      "outputs": [],
      "source": [
        "major_cities_layers = major_cities_item.layers\n",
        "major_cities_layers"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "08de52ff",
      "metadata": {
        "id": "08de52ff"
      },
      "outputs": [],
      "source": [
        "feature_layer = major_cities_layers[0]\n",
        "feature_layer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "251bc0a5",
      "metadata": {
        "id": "251bc0a5"
      },
      "outputs": [],
      "source": [
        "feature_layer.properties.extent"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "dfb50949",
      "metadata": {
        "id": "dfb50949"
      },
      "outputs": [],
      "source": [
        "feature_layer.properties.capabilities"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "799f662d",
      "metadata": {
        "id": "799f662d"
      },
      "outputs": [],
      "source": [
        "feature_layer.properties.drawingInfo.renderer.type"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "daec5b71",
      "metadata": {
        "id": "daec5b71"
      },
      "outputs": [],
      "source": [
        "for f in feature_layer.properties.fields:\n",
        "    print(f['name'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e2fd8291",
      "metadata": {
        "id": "e2fd8291"
      },
      "outputs": [],
      "source": [
        "for f in feature_layer.properties.fields:\n",
        "    print(f['CLASS'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e13c1830",
      "metadata": {
        "id": "e13c1830"
      },
      "outputs": [],
      "source": [
        "# Create a map widget\n",
        "map = gis.map('los angeles') # Passing a place name to the constructor\n",
        "                        # will initialize the extent of the map.\n",
        "map"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ebb631c4",
      "metadata": {
        "scrolled": true,
        "id": "ebb631c4"
      },
      "outputs": [],
      "source": [
        "map = gis.map(\"los angeles\")\n",
        "map.basemap = \"satellite\"\n",
        "map.zoom = 10\n",
        "map"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5b1de393",
      "metadata": {
        "id": "5b1de393"
      },
      "outputs": [],
      "source": [
        "landsat_item = gis.content.search(\"Landsat Multispectral tags:'Landsat on AWS','landsat 9', 'Multispectral', 'Multitemporal', 'imagery', 'temporal', 'MS'\", 'Imagery Layer', outside_org=True)[0]\n",
        "print(landsat_item)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6505ee89",
      "metadata": {
        "id": "6505ee89"
      },
      "outputs": [],
      "source": [
        "map.add_layer(landsat_item)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "24888e40",
      "metadata": {
        "id": "24888e40"
      },
      "outputs": [],
      "source": [
        "map.time_slider"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8ffa45d4",
      "metadata": {
        "id": "8ffa45d4"
      },
      "outputs": [],
      "source": [
        "from datetime import datetime\n",
        "map.time_slider = True\n",
        "map.set_time_extent(start_time=datetime(2021, 12, 12), end_time=datetime(2022, 4, 12), interval=10, unit='days')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "277158e0",
      "metadata": {
        "id": "277158e0"
      },
      "outputs": [],
      "source": [
        "from arcgis.geometry import Point\n",
        "\n",
        "pt = Point({\"x\" : 34.092809, \"y\" : -118.328661, \n",
        "            \"spatialReference\" : {\"wkid\" : 3309}})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0ab84315",
      "metadata": {
        "id": "0ab84315"
      },
      "outputs": [],
      "source": [
        "map.draw(pt)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "42e8d805",
      "metadata": {
        "id": "42e8d805"
      },
      "outputs": [],
      "source": [
        "map.draw('polyline')\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "40e7eb3a",
      "metadata": {
        "id": "40e7eb3a"
      },
      "outputs": [],
      "source": [
        "map.draw('polygon')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "43e1ca29",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {
                "hidden": true
              }
            }
          }
        },
        "id": "43e1ca29"
      },
      "outputs": [],
      "source": [
        "from arcgis.gis import GIS\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eaf47b15",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {
                "hidden": true
              }
            }
          }
        },
        "id": "eaf47b15"
      },
      "outputs": [],
      "source": [
        "gis = GIS()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2d6b30a2",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": true,
        "id": "2d6b30a2"
      },
      "outputs": [],
      "source": [
        "map1 =gis.map(\"Chicago,Illinois\")\n",
        "map1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b33d485a",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b33d485a"
      },
      "outputs": [],
      "source": [
        "#gis = GIS(username ='xxx', password='xxx')\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0430e663",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": true,
        "id": "0430e663"
      },
      "outputs": [],
      "source": [
        "map1 =gis.map(\"Chicago,Illinois\")\n",
        "map1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "01ab5fa4",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": false,
        "id": "01ab5fa4"
      },
      "outputs": [],
      "source": [
        "from IPython.display import display\n",
        "\n",
        "items = gis.content.search('Chicago bike trails', item_type='feature layer')\n",
        "for item in items:\n",
        "    display(item)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f2b15330",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "f2b15330"
      },
      "outputs": [],
      "source": [
        "items"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "220d7a41",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "220d7a41"
      },
      "source": [
        "Chicago Metropolitan Agency for Planning"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "57aeedde",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "57aeedde"
      },
      "outputs": [],
      "source": [
        "# filter out item with title 'Chicago Parks' to add\n",
        "item_to_add = [temp_item for temp_item in items if 'CMAP' in temp_item.title]\n",
        "item_to_add"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d0314a82",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "d0314a82"
      },
      "outputs": [],
      "source": [
        "gis = GIS(api_key=\"yourkey\",\n",
        "              referer=\"https\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5dc8d9dc",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "5dc8d9dc"
      },
      "outputs": [],
      "source": [
        "public_3d_city_scenes = gis.content.search(query=\"3d cities\", item_type = \"web scene\",\n",
        "                                           sort_field=\"numViews\" ,sort_order=\"asc\",\n",
        "                                           max_items = 15, outside_org=True)\n",
        "for item in public_3d_city_scenes:\n",
        "    display(item)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4ccceca6",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "4ccceca6"
      },
      "outputs": [],
      "source": [
        "gis = GIS.users"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "020975ac",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "020975ac"
      },
      "outputs": [],
      "source": [
        "gis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3d723bf8",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": true,
        "id": "3d723bf8"
      },
      "outputs": [],
      "source": [
        "gis = GIS.users\n",
        "gis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "611157ef",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {
                "hidden": true
              }
            }
          }
        },
        "id": "611157ef"
      },
      "outputs": [],
      "source": [
        "from arcgis.gis import GIS\n",
        "from arcgis.geocoding import geocode\n",
        "from arcgis.raster.functions import *\n",
        "from arcgis import geometry\n",
        "import ipywidgets as widgets\n",
        "    \n",
        "import pandas as pd\n",
        "\n",
        "# connect as an anonymous user\n",
        "gis = GIS()\n",
        "\n",
        "# search for the landsat multispectral imagery layer\n",
        "landsat_item = gis.content.search(\"Landsat Multispectral tags:'Landsat on AWS','landsat 9', 'Multispectral', 'Multitemporal', 'imagery', 'temporal', 'MS'\", 'Imagery Layer', outside_org=True)[0]\n",
        "landsat = landsat_item.layers[0]\n",
        "df = None\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f3ac8f0f",
      "metadata": {
        "id": "f3ac8f0f"
      },
      "source": [
        "Setting an area of interest"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a98b8af9",
      "metadata": {
        "id": "a98b8af9"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import apply"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "04d5163d",
      "metadata": {
        "id": "04d5163d"
      },
      "outputs": [],
      "source": [
        "color_infrared = apply(landsat, 'Color Infrared with DRA')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c8950654",
      "metadata": {
        "id": "c8950654"
      },
      "outputs": [],
      "source": [
        "m3 = gis.map('los angeles')\n",
        "m3.add_layer(color_infrared)\n",
        "m3"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a21c5c61",
      "metadata": {
        "id": "a21c5c61"
      },
      "outputs": [],
      "source": [
        "color_infrared"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "44230cd1",
      "metadata": {
        "id": "44230cd1"
      },
      "outputs": [],
      "source": [
        "from arcgis.geocoding import geocode\n",
        "area = geocode('los angeles', out_sr=landsat.properties.spatialReference)[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6b75fac1",
      "metadata": {
        "id": "6b75fac1"
      },
      "outputs": [],
      "source": [
        "color_infrared.extent = area['extent']"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1a650f63",
      "metadata": {
        "id": "1a650f63"
      },
      "source": [
        "Exporting Images from Imagery Layer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "337796fa",
      "metadata": {
        "id": "337796fa"
      },
      "outputs": [],
      "source": [
        "landsat.extent = area['extent']\n",
        "landsat"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "690a9617",
      "metadata": {
        "id": "690a9617"
      },
      "outputs": [],
      "source": [
        "from IPython.display import Image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d062e4ea",
      "metadata": {
        "id": "d062e4ea"
      },
      "outputs": [],
      "source": [
        "img = landsat.export_image(bbox=area['extent'], size=[1200,450], f='image')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cd8bba69",
      "metadata": {
        "id": "cd8bba69"
      },
      "outputs": [],
      "source": [
        "Image(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e01626ba",
      "metadata": {
        "id": "e01626ba"
      },
      "outputs": [],
      "source": [
        "savedimg = landsat.export_image(bbox=area['extent'], size=[1200,450], f='image', save_folder='.', save_file='img.jpg')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0e6a347c",
      "metadata": {
        "id": "0e6a347c"
      },
      "outputs": [],
      "source": [
        "savedimg\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "29a52b44",
      "metadata": {
        "id": "29a52b44"
      },
      "outputs": [],
      "source": [
        "from IPython.display import Image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "092c2c93",
      "metadata": {
        "id": "092c2c93"
      },
      "outputs": [],
      "source": [
        "Image(filename=savedimg, width=1200, height=450)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0a8a423a",
      "metadata": {
        "id": "0a8a423a"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import stretch, extract_band"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "49101622",
      "metadata": {
        "id": "49101622"
      },
      "outputs": [],
      "source": [
        "naturalcolor = stretch(extract_band(landsat, [3,2,1]), \n",
        "                    stretch_type='percentclip', min_percent=0.1, max_percent=0.1, gamma=[1, 1, 1], dra=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0d7cf552",
      "metadata": {
        "id": "0d7cf552"
      },
      "outputs": [],
      "source": [
        "naturalcolor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cd71f82b",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "cd71f82b"
      },
      "outputs": [],
      "source": [
        "from arcgis.features import*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d983aded",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {
                "hidden": false
              }
            }
          }
        },
        "id": "d983aded"
      },
      "outputs": [],
      "source": [
        "landsat_item"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2dac2ee6",
      "metadata": {
        "id": "2dac2ee6"
      },
      "outputs": [],
      "source": [
        "from arcgis.geometry import Geometry, buffer"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "255cc903",
      "metadata": {
        "id": "255cc903"
      },
      "source": [
        "code snippets with less cloud cover"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0fe2b6ae",
      "metadata": {
        "id": "0fe2b6ae"
      },
      "outputs": [],
      "source": [
        "selected = landsat.filter_by(where=\"(Category = 1) AND (CloudCover <=0.10) AND (WRS_Row = 36)\", \n",
        "                   geometry=arcgis.geometry.filters.intersects(area['extent']))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "aedd28f2",
      "metadata": {
        "id": "aedd28f2"
      },
      "outputs": [],
      "source": [
        "fs = selected.query(out_fields=\"AcquisitionDate, GroupName, Best, CloudCover, WRS_Row, Month, Name\", \n",
        "              return_geometry=True,\n",
        "              return_distinct_values=False,\n",
        "              order_by_fields=\"AcquisitionDate\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c5372e26",
      "metadata": {
        "scrolled": true,
        "id": "c5372e26"
      },
      "outputs": [],
      "source": [
        "df = fs.sdf\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8e06a929",
      "metadata": {
        "id": "8e06a929"
      },
      "outputs": [],
      "source": [
        "df = fs.sdf\n",
        "df.tail()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b569bbc6",
      "metadata": {
        "id": "b569bbc6"
      },
      "outputs": [],
      "source": [
        "df.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f7086379",
      "metadata": {
        "id": "f7086379"
      },
      "outputs": [],
      "source": [
        "df['Time'] = pd.to_datetime(df['AcquisitionDate'], unit='ms')\n",
        "df['Time'].head(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ebe8a22e",
      "metadata": {
        "id": "ebe8a22e"
      },
      "outputs": [],
      "source": [
        "m3 = gis.map('los angeles', 7)\n",
        "display(m3)\n",
        "m3.add_layer(selected.last())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a534558d",
      "metadata": {
        "scrolled": true,
        "id": "a534558d"
      },
      "outputs": [],
      "source": [
        "m3 = gis.map('los angeles', 7)\n",
        "display(m3)\n",
        "m3.add_layer(selected.first())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "68a500bd",
      "metadata": {
        "id": "68a500bd"
      },
      "outputs": [],
      "source": [
        "old = landsat.filter_by('OBJECTID=3106827')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d33cd21d",
      "metadata": {
        "id": "d33cd21d"
      },
      "outputs": [],
      "source": [
        "new = landsat.filter_by('OBJECTID=3558253')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ccc097aa",
      "metadata": {
        "id": "ccc097aa"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import *"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f149fd3e",
      "metadata": {
        "id": "f149fd3e"
      },
      "outputs": [],
      "source": [
        "diff = stretch(composite_band([ndvi(old, '5 4'),\n",
        "                               ndvi(new, '5 4'),\n",
        "                               ndvi(old, '5 4')]), \n",
        "                               stretch_type='stddev',  num_stddev=2.5, min=0, max=255, dra=True, astype='u8')\n",
        "diff"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "790c4544",
      "metadata": {
        "id": "790c4544"
      },
      "outputs": [],
      "source": [
        "ndvi_diff = ndvi(new, '5 4') - ndvi(old, '5 4')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2b967efb",
      "metadata": {
        "id": "2b967efb"
      },
      "outputs": [],
      "source": [
        "ndvi_diff"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "55003a7e",
      "metadata": {
        "id": "55003a7e"
      },
      "outputs": [],
      "source": [
        "threshold_val = 0.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b553c18e",
      "metadata": {
        "id": "b553c18e"
      },
      "outputs": [],
      "source": [
        "masked = colormap(remap(ndvi_diff, \n",
        "                        input_ranges=[threshold_val, 1], \n",
        "                        output_values=[1], \n",
        "                        no_data_ranges=[-1, threshold_val], astype='u8'), \n",
        "                  colormap=[[1, 124, 252, 0]], astype='u8')\n",
        "\n",
        "Image(masked.export_image(bbox=area['extent'], size=[1200,450], f='image'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b2253681",
      "metadata": {
        "id": "b2253681"
      },
      "outputs": [],
      "source": [
        "m = gis.map('los angeles')\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8b129a21",
      "metadata": {
        "id": "8b129a21"
      },
      "outputs": [],
      "source": [
        "\n",
        "m.add_layer(diff)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "13de7589",
      "metadata": {
        "id": "13de7589"
      },
      "outputs": [],
      "source": [
        "\n",
        "m.add_layer(masked)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2c15042f",
      "metadata": {
        "id": "2c15042f"
      },
      "source": [
        "BAck to old section"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "01706141",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": false,
        "id": "01706141"
      },
      "outputs": [],
      "source": [
        "from IPython.display import HTML\n",
        "HTML(landsat_item.description)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5a5883ae",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "5a5883ae"
      },
      "outputs": [],
      "source": [
        "landsat = landsat_item.layers[0]\n",
        "landsat"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "072c4079",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "072c4079"
      },
      "outputs": [],
      "source": [
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "70fb3a57",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "70fb3a57"
      },
      "outputs": [],
      "source": [
        "pd.DataFrame(landsat.key_properties()['BandProperties'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "035eed27",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "035eed27"
      },
      "outputs": [],
      "source": [
        "m = gis.map('los angeles')\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "786dda5a",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "786dda5a"
      },
      "outputs": [],
      "source": [
        "m.add_layer(landsat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "53ce440c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "53ce440c"
      },
      "outputs": [],
      "source": [
        "for rasterfunc in landsat.properties.rasterFunctionInfos:\n",
        "    print(rasterfunc.name)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8ae577bb",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "8ae577bb"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import apply"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "51bae9ee",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "51bae9ee"
      },
      "outputs": [],
      "source": [
        "color_infrared = apply(landsat, 'Color Infrared with DRA')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d205d91a",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "d205d91a"
      },
      "outputs": [],
      "source": [
        "\n",
        "m = gis.map('los angeles')\n",
        "m.add_layer(color_infrared)\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "99ba8335",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "99ba8335"
      },
      "outputs": [],
      "source": [
        "color_infrared"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b158572a",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b158572a"
      },
      "outputs": [],
      "source": [
        "from arcgis.geocoding import geocode\n",
        "area = geocode('los angeles', out_sr=landsat.properties.spatialReference)[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6b0ef59c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "6b0ef59c"
      },
      "outputs": [],
      "source": [
        "color_infrared.extent = area['extent']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1b198b22",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "1b198b22"
      },
      "outputs": [],
      "source": [
        "color_infrared"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b2b28e26",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b2b28e26"
      },
      "outputs": [],
      "source": [
        "landsat.extent = area['extent']\n",
        "landsat"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a531c8e9",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "a531c8e9"
      },
      "source": [
        "Exporting Images from Imagery Layer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5c835bd7",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "5c835bd7"
      },
      "outputs": [],
      "source": [
        "from IPython.display import Image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "defebeaa",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "defebeaa"
      },
      "outputs": [],
      "source": [
        "\n",
        "img = landsat.export_image(bbox=area['extent'], size=[1200,450], f='image')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b9860aca",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b9860aca"
      },
      "outputs": [],
      "source": [
        "Image(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6f2239a6",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "6f2239a6"
      },
      "outputs": [],
      "source": [
        "savedimg = landsat.export_image(bbox=area['extent'], size=[1200,450], f='image', save_folder='.', save_file='img.jpg')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d804af4c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "d804af4c"
      },
      "outputs": [],
      "source": [
        "savedimg\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a86f634c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "a86f634c"
      },
      "outputs": [],
      "source": [
        "from IPython.display import Image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7c16ad3c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "7c16ad3c"
      },
      "outputs": [],
      "source": [
        "Image(filename=savedimg, width=1200, height=450)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "63002649",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "63002649"
      },
      "source": [
        "Exporting images from an imagery layer to which a raster function has been applied"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b9aed694",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b9aed694"
      },
      "outputs": [],
      "source": [
        "color_infrared = apply(landsat, 'Color Infrared with DRA')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f91f7f63",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "f91f7f63"
      },
      "outputs": [],
      "source": [
        "\n",
        "img = color_infrared.export_image(bbox=area['extent'], size=[1200, 450], f='image')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9428da9f",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "9428da9f"
      },
      "outputs": [],
      "source": [
        "\n",
        "Image(img)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "34818894",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "34818894"
      },
      "source": [
        "Vegetation Index\n",
        "An index combines two or more wavelengths to indicate the relative abundance of different land cover features, like vegetation or moisture.\n",
        "\n",
        "Let's use the 'NDVI Colorized' raster function to visualize healthy, green vegetation:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ab3b8b24",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "ab3b8b24"
      },
      "outputs": [],
      "source": [
        "ndvi_colorized = apply(landsat, 'NDVI Colorized')\n",
        "ndvi_colorized"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fa1d98ec",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "fa1d98ec"
      },
      "source": [
        "Custom Bands\n",
        "You can also create your own indexes and band combinations, as well as specify stretch and gamma values to adjust the image contrast.\n",
        "\n",
        "The code below first extracts the [3 (Red), 2 (Green), 1 (Blue)] bands using the extract_bands function and passes it's output to the stretch function to enhance the image:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a6c995f7",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "a6c995f7"
      },
      "outputs": [],
      "source": [
        "\n",
        "from arcgis.raster.functions import stretch, extract_band\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "57329037",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "57329037"
      },
      "outputs": [],
      "source": [
        "naturalcolor = stretch(extract_band(landsat, [3,2,1]), \n",
        "                    stretch_type='percentclip', min_percent=0.1, max_percent=0.1, gamma=[1, 1, 1], dra=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6cc5873c",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "6cc5873c"
      },
      "outputs": [],
      "source": [
        "naturalcolor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7830173a",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "7830173a"
      },
      "outputs": [],
      "source": [
        "import arcgis\n",
        "g = arcgis.geometry.Geometry(area['extent'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a3694614",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "a3694614"
      },
      "outputs": [],
      "source": [
        "samples = landsat.get_samples(g, sample_count=50,\n",
        "                                 out_fields='AcquisitionDate,OBJECTID,GroupName,Category,SunAzimuth,SunElevation,CloudCover')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c7bc5c74",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "c7bc5c74"
      },
      "outputs": [],
      "source": [
        "samples[10]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "652e71bd",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "652e71bd"
      },
      "outputs": [],
      "source": [
        "import datetime\n",
        "value = samples[0]['attributes']['AcquisitionDate']\n",
        "datetime.datetime.fromtimestamp(value /1000).strftime(\"Acquisition Date: %d %b, %Y\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0ab08451",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "0ab08451"
      },
      "outputs": [],
      "source": [
        "pd.DataFrame(samples[0]['attributes'], index=[0])"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e5190376",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "e5190376"
      },
      "source": [
        "Spectral profile from the sampled values at a location"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b0bd94be",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "scrolled": true,
        "id": "b0bd94be"
      },
      "outputs": [],
      "source": [
        "m = gis.map('los angeles')\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "801e433b",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "801e433b"
      },
      "outputs": [],
      "source": [
        "m.add_layer(landsat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8e16b199",
      "metadata": {
        "id": "8e16b199"
      },
      "outputs": [],
      "source": [
        "from arcgis.gis import GIS\n",
        "gis=GIS()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4dc43dfb",
      "metadata": {
        "id": "4dc43dfb"
      },
      "outputs": [],
      "source": [
        "landsat_item = gis.content.search('Multispectral Landsat', 'Imagery Layer', outside_org=True)[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "99ff5dd5",
      "metadata": {
        "id": "99ff5dd5"
      },
      "outputs": [],
      "source": [
        "m = gis.map('los angeles')\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "652d35d3",
      "metadata": {
        "scrolled": false,
        "id": "652d35d3"
      },
      "outputs": [],
      "source": [
        "landsat = landsat_item.layers[0]\n",
        "landsat"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e2832768",
      "metadata": {
        "id": "e2832768"
      },
      "outputs": [],
      "source": [
        "m.add_layer(landsat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b11274ad",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "b11274ad"
      },
      "outputs": [],
      "source": [
        "from bokeh.models import Range1d\n",
        "from bokeh.plotting import figure, show, output_notebook\n",
        "from IPython.display import clear_output\n",
        "output_notebook()\n",
        "\n",
        "def get_samples(mw, g):\n",
        "    clear_output()\n",
        "    m.draw(g)\n",
        "    samples = landsat.get_samples(g, pixel_size=30)\n",
        "    values = samples[0]['value']\n",
        "    vals = [float(int(s)/100000) for s in values.split(' ')]\n",
        "    \n",
        "    x = ['1','2', '3','4','5','6','7','8']\n",
        "    y = vals\n",
        "    p = figure(title=\"Spectral Profile\", x_axis_label='Spectral Bands', y_axis_label='Data Values', width=600, height=300)\n",
        "    p.line(x, y, legend_label=\"Selected Point\", line_color=\"red\", line_width=2)\n",
        "    p.circle(x, y, line_color=\"red\", fill_color=\"white\", size=8)\n",
        "    p.y_range=Range1d(0, 1.0)\n",
        "\n",
        "    show(p)\n",
        "    \n",
        "print('Click anywhere on the map to plot the spectral profile for that location.')\n",
        "m.on_click(get_samples)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "10132cd8",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "10132cd8"
      },
      "outputs": [],
      "source": [
        "items = gis.content.search(\"title: Multispectral Landsat\", item_type=\"Imagery Layer\",  outside_org=True)\n",
        "items[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "51f5042b",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "51f5042b"
      },
      "outputs": [],
      "source": [
        "l9_lyr = items[0].layers[0]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "38aa5b61",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "38aa5b61"
      },
      "outputs": [],
      "source": [
        "land_water_lr = stretch(extract_band(l9_lyr, [4, 5, 3]),\n",
        "                        stretch_type='PercentClip',\n",
        "                        min_percent=2, \n",
        "                        max_percent=2,\n",
        "                        dra=True, \n",
        "                        gamma=[1, 1, 1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "159f9bab",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "159f9bab"
      },
      "outputs": [],
      "source": [
        "landmap = gis.map('los angeles')\n",
        "landmap"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8fa93687",
      "metadata": {
        "id": "8fa93687"
      },
      "outputs": [],
      "source": [
        "ndvi_colorized = apply(landsat, 'NDVI Colorized')\n",
        "ndvi_colorized"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f7f92eb7",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "f7f92eb7"
      },
      "outputs": [],
      "source": [
        "landmap.add_layer(land_water_lr)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "abd9c93e",
      "metadata": {
        "id": "abd9c93e"
      },
      "outputs": [],
      "source": [
        "!pip install graphviz"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c88ceb03",
      "metadata": {
        "id": "c88ceb03"
      },
      "outputs": [],
      "source": [
        "import graphviz"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7a5b7746",
      "metadata": {
        "extensions": {
          "jupyter_dashboards": {
            "version": 1,
            "views": {
              "grid_default": {},
              "report_default": {}
            }
          }
        },
        "id": "7a5b7746"
      },
      "outputs": [],
      "source": [
        "land_water_lr.draw_graph()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a85b518b",
      "metadata": {
        "id": "a85b518b"
      },
      "outputs": [],
      "source": [
        "from arcgis.geocoding import geocode\n",
        "\n",
        "# get spatial extent of los angeles\n",
        "area = geocode('los angeles', out_sr=l9_lyr.properties.spatialReference)[0]\n",
        "area['extent']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b3b467a1",
      "metadata": {
        "id": "b3b467a1"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "from datetime import datetime\n",
        "\n",
        "selected = l9_lyr.filter_by(time=[datetime(2019, 1, 1), datetime(2022, 1, 1)],\n",
        "                             geometry=arcgis.geometry.filters.intersects(area['extent']))\n",
        "\n",
        "df = selected.query(out_fields=\"AcquisitionDate, GroupName, CloudCover\", \n",
        "                    order_by_fields=\"AcquisitionDate\").sdf\n",
        "df.head(50)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e13abdc6",
      "metadata": {
        "id": "e13abdc6"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "from datetime import datetime\n",
        "\n",
        "selected = l9_lyr.filter_by(time=[datetime(2019, 1, 1), datetime(2022, 1, 1)],\n",
        "                             geometry=arcgis.geometry.filters.intersects(area['extent']))\n",
        "\n",
        "df = selected.query(out_fields=\"AcquisitionDate, GroupName, CloudCover\", \n",
        "                    order_by_fields=\"AcquisitionDate\").sdf\n",
        "df.tail(50)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8276f9e2",
      "metadata": {
        "id": "8276f9e2"
      },
      "outputs": [],
      "source": [
        "df.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ff184e07",
      "metadata": {
        "id": "ff184e07"
      },
      "outputs": [],
      "source": [
        "df['Time'] = pd.to_datetime(df['AcquisitionDate'], unit='ms')\n",
        "df['Time'].head(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e540b8a5",
      "metadata": {
        "id": "e540b8a5"
      },
      "outputs": [],
      "source": [
        "m = gis.map('Los Angeles', 15)\n",
        "display(m)\n",
        "m.add_layer(selected.last())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2946b24b",
      "metadata": {
        "id": "2946b24b"
      },
      "outputs": [],
      "source": [
        "m = gis.map('Los Angeles', 15)\n",
        "display(m)\n",
        "m.add_layer(selected.first())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3b4279bf",
      "metadata": {
        "id": "3b4279bf"
      },
      "outputs": [],
      "source": [
        "old = landsat.filter_by('OBJECTID=610881')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bf318c9d",
      "metadata": {
        "id": "bf318c9d"
      },
      "outputs": [],
      "source": [
        "new = landsat.filter_by('OBJECTID=3434313')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6a8bbeac",
      "metadata": {
        "id": "6a8bbeac"
      },
      "outputs": [],
      "source": [
        "from arcgis.raster.functions import *\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e470cd00",
      "metadata": {
        "id": "e470cd00"
      },
      "outputs": [],
      "source": [
        "diff = stretch(composite_band([ndvi(old, '5 4'),\n",
        "                               ndvi(new, '5 4'),\n",
        "                               ndvi(old, '5 4')]), \n",
        "                               stretch_type='stddev',  num_stddev=2.5, min=0, max=200, dra=True, astype='u8')\n",
        "diff"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "329cdb86",
      "metadata": {
        "id": "329cdb86"
      },
      "outputs": [],
      "source": [
        "ndvi_diff = ndvi(new, '5 4') - ndvi(old, '5 4')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8a805a1b",
      "metadata": {
        "id": "8a805a1b"
      },
      "outputs": [],
      "source": [
        "ndvi_diff"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "31df9df5",
      "metadata": {
        "id": "31df9df5"
      },
      "outputs": [],
      "source": [
        "\n",
        "threshold_val = 0.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "62cd2345",
      "metadata": {
        "id": "62cd2345"
      },
      "outputs": [],
      "source": [
        "masked = colormap(remap(ndvi_diff, \n",
        "                        input_ranges=[threshold_val, 1], \n",
        "                        output_values=[1], \n",
        "                        no_data_ranges=[-1, threshold_val], astype='u8'), \n",
        "                  colormap=[[1, 124, 252, 0]], astype='u8')\n",
        "\n",
        "Image(masked.export_image(bbox=area['extent'], size=[1200,450], f='image'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "49f5d158",
      "metadata": {
        "id": "49f5d158"
      },
      "outputs": [],
      "source": [
        "m = gis.map('Los Angeles')\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "880dee01",
      "metadata": {
        "id": "880dee01"
      },
      "outputs": [],
      "source": [
        "m.add_layer(diff)\n",
        "m"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a9f70c91",
      "metadata": {
        "id": "a9f70c91"
      },
      "outputs": [],
      "source": [
        "m.add_layer(masked)\n",
        "m"
      ]
    }
  ],
  "metadata": {
    "extensions": {
      "jupyter_dashboards": {
        "activeView": "grid_default",
        "version": 1,
        "views": {
          "grid_default": {
            "cellMargin": 10,
            "defaultCellHeight": 20,
            "maxColumns": 12,
            "name": "grid",
            "type": "grid"
          },
          "report_default": {
            "name": "report",
            "type": "report"
          }
        }
      }
    },
    "hide_input": false,
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": false,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": false
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
